/**
  ETFRoot project, Anyang Normal University && IMP-CAS
  \class ETFUserUnpackTask
  \brief user class for tasks in the tracking stage
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-10-31
  \date 2022-04-27 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2021-10-31   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <TH1F.h>
#include <TH2F.h>
#include <TFolder.h>
#include "ETFUserAnaTask.h"

ClassImp(ETFUserAnaTask);

ETFUserAnaTask::ETFUserAnaTask(const char *name, int verbose) : ETFAnaTask(name, verbose){}

ETFUserAnaTask::~ETFUserAnaTask(){}


//////////////////////   Let's create some histograms   //////////////////////
void ETFUserAnaTask::CreateHistos(){
  ETFAnaTask::CreateHistos();
  fFolder->SetName("UserAnaTask");

  TFolder *fDCDis = new TFolder("DCDis", "Hit Distribution of the DCs");
  TFolder *fDCToRef = new TFolder("DCToRef", "Time w.r.t. Tstop for the DCs");
  TFolder *fDCMulti = new TFolder("DCMul", "Multiplicity of the DCs");
  TFolder *fDT = new TFolder("DT", "Drift Time of the DCs Extracted from Fitted Tracks");
  fFolder->SetOwner(); // objects are owned by the folder, and deleted along with it
  fDCDis->SetOwner(); fDCToRef->SetOwner(); fDCMulti->SetOwner(); fDT->SetOwner();

  hTOFWFiredDist = new TH1F("hTOFWFiredDistDCArr", // TOFWall fired distribution
    "Fired TOF Wall Strip Distribution;Strip Serial Id", 33, -1.5, 31.5);
  hTOFWMulti = new TH1F("hTOFWMultiDCArr", // TOFWall hit multiplicity
    "TOF Wall Hit Multiplicity;multiplicity", 17, -1.5, 15.5);
  hTOFWHitPos = new TH2F("hTOFWHitPosDCArr", // TOFWall hit position, utilizing both-end readouts
    "TOF Wall Hit Position;Strip Serial Id;Hit Pos (from Down end) [mm]", 33, -1.5, 31.5, 500, -300., 1700.);
  hTOFWHitPosCmp = new TH2F("hTOFWHitPosCmpDCArr",
    "TOF Wall Hit Position Compare;X - time difference method [mm];Y - Calculated from 3D tracks [mm]",
    500, -300., 1700., 500, -300., 1700.);
  hTOFWToTRef = new TH2F("hTOFWToTRef", // TOFWall time to T-reference
    "hTOFWToTRef;stripId;timeToTRef [ns]", 33, -1.5, 31.5, 30000, -200., 800.);
  fFolder->Add(hTOFWFiredDist); fFolder->Add(hTOFWMulti); fFolder->Add(hTOFWHitPos);
  fFolder->Add(hTOFWHitPosCmp); fFolder->Add(hTOFWToTRef);
  const char *xuv = "XUV";
  char name[64], title[128];
  for(int j = 0; j < 3; j++){ // loop over the three MWDCs
    for(int k = 0; k < 3; k++){ // loop over XUV SLayers
      for(int l = 0; l < 2; l++){ // loop over X[UV]1-2
        // DC hit multiplicity
        sprintf(name, "hDCMultiDC%d_%c%d", j, xuv[k], l);
        sprintf(title, "DC Hit Multiplicity - DC%d%c%d;multiplicity", j, xuv[k], l);
        hDCMulti[j][k][l] = new TH1F(name, title, 35, -4.5, 30.5);
        fDCMulti->Add(hDCMulti[j][k][l]);
      } // end for over layer 1 and 2
      // DC fired distribution
      sprintf(name, "hDCFiredDistDC%d_%c", j, xuv[k]);
      sprintf(title, "Fired DC Anode Distribution - DC%d%c;Wire Serial Id", j, xuv[k]);
      hDCFiredDist[j][k] = new TH1F(name, title, 205, -4.5, 200.5);
      fDCDis->Add(hDCFiredDist[j][k]);
      // DC drift time distribution
      sprintf(name, "HdtDC%d_%c", j, xuv[k]);
      sprintf(title, "Drift Time Distribution - DC%d%c;drift time [ns]", j, xuv[k]);
      hdt[j][k] = new TH1F(name, title, 500, -100., 400.);
      fDT->Add(hdt[j][k]);
      // DC time to T-reference
      sprintf(name, "hDCToTRefDC%d_%c", j, xuv[k]);
      sprintf(title, "DC Time to T-reterence - DC%d%c;timeToTRef [ns]", j, xuv[k]);
      hDCToTRef[j][k] = new TH1F(name, title, 4000, -2000., 2000.);
      fDCToRef->Add(hDCToTRef[j][k]);
    } // end for over X-U-V
  } // end for over DCs
  fFolder->Add(hTOFWFiredDist); fFolder->Add(fDCDis); fFolder->Add(hTOFWToTRef);
  fFolder->Add(fDCToRef); fFolder->Add(hTOFWMulti); fFolder->Add(fDCMulti);
  fFolder->Add(hTOFWHitPos); fFolder->Add(hTOFWHitPosCmp); fFolder->Add(fDT);
} // end member function CreateHistos
